package com.laiyizhan.cms.first.service.impl;

import com.laiyizhan.cms.first.request.SixthRequest;
import com.laiyizhan.cms.first.service.ISixthService;
import com.laiyizhan.common.mapper.*;
import com.laiyizhan.common.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import static org.springframework.beans.BeanUtils.copyProperties;

/**
 * Created by SoulWolf on 2017/7/21.
 */
@Service
public class SixthServiceImpl implements ISixthService {

@Autowired
    StateMapper stateMapper;
    @Autowired
    Sixth_messageMapper sixth_messageMapper;
    @Autowired
    Sixth_aMapper sixth_aMapper;
    @Autowired
    Sixth_bMapper sixth_bMapper;
    @Autowired
    Sixth_cMapper sixth_cMapper;
    @Autowired
    Sixth_dMapper sixth_dMapper;
    @Autowired
    Sixth_eMapper sixth_eMapper;


    public State Sixth_index(SixthRequest request) {
        State temp = stateMapper.selectByName(request.getoName());
        if (temp == null) {
            State state = new State();
            state.setHospital(request.getoName());
            state.setA("0");
            state.setB("0");
            state.setC("0");
            state.setD("0");
            state.setE("0");
            state.setF("0");
            state.setG("0");
            stateMapper.insertSelective(state);
            return state;
        }else {
            return stateMapper.selectByName(request.getoName());
        }
    }

    private String[] a_c = {"高级职称","中级职称","初级职称","其他","合计"};
    private String[] a_h = {"年平均在职职工人数（人）","卫生技术人员（人）","其他技术人员（人）","管理人员（人）","工勤技能人员 (人)","临时工（人）（选填）","在职职工年人均工资（万元/人）"};

    private String[] b_c = {"各级政府","国际组织","国际社会组织"};

    private String[] c_h = {"年度收费总额（元）","服务量（人次）","次均收费水平（元/人次）","卫生材料支出（元）","药品支出（元）"};


    public String Sixth_a(SixthRequest request){
        Check_message("a",request);
        Sixth_message sixth_message = new Sixth_message();
        copyProperties
                (request,sixth_message);
        sixth_messageMapper.insertSelective(sixth_message);

       /*数据处理*/
        float[] temp =  new float[35];
        for(int i=0;i<35;i++){
            if (request.getSixthA()[i]!=""){
                temp[i] = Float.parseFloat(request.getSixthA()[i]);
            }
        }
        /*计算前五行的第一列合计*/
        for(int l=0;l<28;l=l+7){
            temp[l]=temp[l+1]+temp[l+2]+temp[l+3]+temp[l+4];
               /*新添加的验证，当人数不为0的时候，工资不能为0*/
            if(temp[l]!=0 || temp[l+5]!=0 ){
                if( temp[l+6]==0){
                    return "no";
                }
            }
        }
        /*计算第六行的前五列合计*/
        for(int i=0;i<6;i++){
            temp[i+28]=temp[i+7]+temp[i+14]+temp[i+21]+temp[i];
        }


        /*生成对象，存入数据库*/
        int num=0;
        List<Sixth_a> list = new ArrayList<Sixth_a>();
        for(int i=0;i<5;i++) {
            for (int j = 0; j < 7; j++) {
                Sixth_a sixth_a = new Sixth_a();
                sixth_a.setName(request.getoName());
                sixth_a.setType(a_c[i]);
                sixth_a.setcType(a_h[j]);
                sixth_a.setCount(String.valueOf(temp[num]));
                list.add(sixth_a);
                num=num+1;
            }

        }


        sixth_aMapper.insertBatch(list);
        Change_state("a",request.getoName());
        return "ok";
    }

    public String Sixth_b(SixthRequest request){
        Check_message("b",request);
        if(request.getSixthB()!=null) {
            request.getSixthB()[0] = "本机构财政项目支出补助收入";
        }
        /*数据处理*/
        float b_count=0F;
        for(int i=1;i<52;i=i+2){
            if(request.getSixthB()[i]!=""){
                b_count=b_count+Float.parseFloat(request.getSixthB()[i]);
            }
        }


        List<Sixth_b> list = new ArrayList<Sixth_b>();
        if(request.getSixthB()!=null){
            request.getSixthB()[1]=String.valueOf(b_count);
            for(int j=0;j<72;j=j+2){
                Sixth_b sixth_b = new Sixth_b();
                /*医院名称固定，由前一页传递过来*/
                sixth_b.setHosptial(request.getoName());
                if(j<52){
                    sixth_b.setSource(b_c[0]);
                }else if(j<62){
                    sixth_b.setSource(b_c[1]);
                }else {
                    sixth_b.setSource(b_c[2]);
                }
                sixth_b.setProjectName(request.getSixthB()[j]);
                sixth_b.setCount(request.getSixthB()[j+1]);
                list.add(sixth_b);
            }
            sixth_bMapper.insertBatch(list);
            Change_state("b",request.getoName());
        }
        return "ok";
    }

    public String Sixth_c(SixthRequest request){
        Check_message("c",request);
        /*先把前台传过来的数据整合*/
        String [] all_sixth_c = new String[180];//字符串型的所有数据
        String[] all_out_c = new String [30];
        float[] c_f = new float[180];//浮点型的所有数据，汉字的部分默认0.0
        float[] new_out = new float[30];//新增的数据 支出情况
        int sad=0;
        for(int i = 0;i<180;i++){
            all_sixth_c[i]=request.getSixthC()[i];
            if((i%6)!=0 && all_sixth_c[i]!=""){c_f[i]=Float.parseFloat(all_sixth_c[i]);}
        }
        for(int w=1;w<30;w++){
            all_out_c[w]=request.getSixthCl()[w];
            if(all_out_c[w]!=null && all_out_c[w]!="" ){new_out[w]=Float.parseFloat(request.getSixthCl()[w]);}
        }
        /*计算必须项的合计数据*/

        /*计算第一列的合计*/
        for(int m=0;m<180;m=m+6){
            if(all_sixth_c[m]!=""){
                c_f[m+1]=c_f[m+2]*c_f[m+3];
            }
        }
        /*计算行的合计  包含了新增的支出列*/
        int[] start = {1,25,73};
        int[] round = {3,7,4};
        int[] out = {0,4,12};
        for(int i=0;i<3;i++){
            for(int j=0;j<5;j++){
                for(int k=0;k<round[i];k++){
                    c_f[start[i]+j]=c_f[start[i]+j]+c_f[(start[i]+j)+6*(k+1)];
                }
            }
            for(int m=0;m<round[i];m++){
                new_out[out[i]]=new_out[out[i]]+new_out[out[i]+m+1];
            }
        }

        /*转为字符串类型赋值给all_sixth_c*/
        for(int n=0;n<180;n++){
            if((n%6)!=0 && n<150){
                all_sixth_c[n]=String.valueOf(c_f[n]);
            }
            if(n>149 && (n%6)==0 && all_sixth_c[n]!=""){
                for(int p=1;p<6;p++){
                    all_sixth_c[n+p]=String.valueOf(c_f[n+p]);
                }
            }
            if(n<25){all_out_c[n]=String.valueOf(new_out[n]);}
        }
        for(int u=0;u<3;u++){
            all_sixth_c[start[u]+1]="";
        }
        for(int u=0;u<3;u++){
            all_sixth_c[start[u]+2]="";
        }

        /*将数据生成对象存储到数据库*/
        List<Sixth_c> list = new ArrayList<Sixth_c>();
        for(int i=0;i<180;i=i+6){
            for(int j=0;j<5;j++) {
                Sixth_c sixth_c = new Sixth_c();
                sixth_c.setType(all_sixth_c[i]);
                sixth_c.setHospital(request.getoName());
                sixth_c.setDepartment(request.getSixthCdepartment());
                sixth_c.setcType(c_h[j]);
                sixth_c.setCount(all_sixth_c[i+j+1]);
                list.add(sixth_c);
            }
           /*增加 支出列至list*/
            Sixth_c sixth_c = new Sixth_c();
            sixth_c.setType(all_sixth_c[i]);
            sixth_c.setHospital(request.getoName());
            sixth_c.setDepartment(request.getSixthCdepartment());
            sixth_c.setcType("其他支出（元）");
            sixth_c.setCount(String.valueOf(all_out_c[i/6]));
            list.add(sixth_c);
        }
        sixth_cMapper.insertBatch(list);
        Change_state("c",request.getoName());
        return "ok";
    }

    public String Sixth_d(SixthRequest request) {
        Check_message("d",request);
        List<Sixth_d> list = new ArrayList<Sixth_d>();
        if(request.getSixthD()!=null){
            for(int i=0;i<40;i=i+2){
                Sixth_d sixth_d = new Sixth_d();
                sixth_d.setHospital(request.getoName());
                sixth_d.setName(request.getSixthD()[i]);
                if (i < 30) {
                    if(request.getSixthD()[i+1]!=""){sixth_d.setCount(request.getSixthD()[i+1]);}
                    else{sixth_d.setCount("0");}
                }else{
                    sixth_d.setCount(request.getSixthD()[i+1]);
                }

                list.add(sixth_d);
            }
        }
        sixth_dMapper.insertBatch(list);
        Change_state("d",request.getoName());
        return "ok";
    }

    public String Sixth_e(SixthRequest request) {
        Check_message("e",request);
        List<Sixth_e> list = new ArrayList<Sixth_e>();

        for (int i = 0; i < 104; i = i + 4) {
            Sixth_e sixth_e = new Sixth_e();
            sixth_e.setHospital(request.getoName());
            sixth_e.setName(request.getSixthE()[i]);
            sixth_e.setType(request.getSixthE()[i + 1]);
            if (i < 44) {
                if(request.getSixthE()[i+2]!=""){
                    sixth_e.setCount(request.getSixthE()[i+2]);
                } else{
                    sixth_e.setCount("0.0");
                }
                if(request.getSixthE()[i+3]!="") {
                    sixth_e.setPrice(request.getSixthE()[i + 3]);
                }else{
                    sixth_e.setPrice("0.0");
                }
            }else{
                sixth_e.setCount(request.getSixthE()[i+2]);
                sixth_e.setPrice(request.getSixthE()[i + 3]);
            }
            list.add(sixth_e);
        }
        sixth_eMapper.insertBatch(list);
        Change_state("e",request.getoName());
        return "ok";
    }

    public String Check_message(String table,SixthRequest request){
        switch (table)
        {
            case "a":
                if(sixth_aMapper.selectByName(request.getoName())!=null){
                    sixth_aMapper.deleteByName(request.getoName());
                }
                break;
            case "b":
                if(sixth_bMapper.selectByName(request.getoName())!=null){
                    sixth_bMapper.deleteByName(request.getoName());
                }
                break;
            case "c":
                Sixth_c sixth_c = new Sixth_c();
                sixth_c.setHospital(request.getoName());
                sixth_c.setDepartment(request.getSixthCdepartment());
                if(sixth_cMapper.selectByName(sixth_c)!=null){
                    sixth_cMapper.deleteByName(sixth_c);
                }
                break;
            case "d":
                if(sixth_dMapper.selectByName(request.getoName())!=null){
                    sixth_dMapper.deleteByName(request.getoName());
                }
                break;
            case "e":
                if(sixth_eMapper.selectByName(request.getoName())!=null){
                    sixth_eMapper.deleteByName(request.getoName());
                }
                break;
        }

        return "ok";
    }


    private String Change_state(String table,String oName) {
        State state = stateMapper.selectByName(oName);
        switch (table)
        {
            case "a":
                state.setA("1");
                break;
            case "b":
                state.setB("1");
                break;
            case "c":
                state.setC("1");
                break;
            case "d":
                state.setD("1");
                break;
            case "e":
                state.setE("1");
                break;
            case "f":
                state.setF("1");
                break;
            case "g":
                state.setG("1");
                break;
        }
        stateMapper.updateByName(state);
        return "ok";
    }



}
